console.log('tax injected');
import { fillcwbb, getTaxCwbbinstitutionCode } from './options/cwbb.js';
import { fillhsqj } from './options/hsqj.js';
import { autoInputAccount, getNationData } from './utils.js';
import { fillInputDone } from "../common.js"

$(document).ready(function () {


  window.postMessage({
    event: 'getBaseInfo'
  }, '*');

  getTaxCwbbinstitutionCode.call(this);


  if (window.location.href.includes('/portal/include.do')) {
    let contentiframe = $('#contentiframe')[0].contentWindow;
    let iframeWindow = contentiframe.$('#mainIframe')[0].contentWindow;
    let institutionCode = 1;
    let input = iframeWindow.$('.billContentBox > .topbox > .rightbox > .type');
    if (input.length > 0) {
      let text = input.text();
      if (text.includes('适用执行小企业会计准则的企业')) {
        institutionCode = 2;
      }
      window.postMessage(
        {
          event: 'fixTaxGovInfo',
          data: {
            institutionCode
          }
        }
      )
    }
  }
});

window.addEventListener('message', async function ({ data }) {
  // 只监听来自于content script的信息
  if (data.sender !== 'content-script') {
    return false;
  }
  if (data.event === 'setSaasData') {
    data = data.data;
    console.log(data);
    let sheet = data.saasData.sheet;
    let sheetType = data.saasData.sheetType
    if (sheet == 'sds' || sheet == 'zzs') {
      // fillInput(data);
    } else if (sheetType.includes('cwbb')) {
      // fillInput(await transformDataByOpenWeb(data));
      fillcwbb(data);
    } else if (sheetType.includes('hsqj')) {
      fillhsqj(data);
    } else {
      fillInputDone({
        success: false,
        tax: sheet,
        message: '暂不支持的税种'
      })
    }
  }
  if (data.event === 'getNationData') {
    getNationData(data.data.taxType);
  }

  if (data.event === 'getBaseInfo') {
    window.baseInfoCache = data.data;

    let pathName = window.location.pathname;
    // 登录填写账号密码
    if (/\/sso\/login/g.test(pathName)) {
      autoInputAccount(baseInfoCache);
    }

  }

}, false);
